﻿using DXBase;
using DXBase.D3D;
using DXBase.Structs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXBase.Samples
{
	public class SpaceBody
	{
		public SpaceBody()
		{
			Satellites = new List<SpaceBody>();
			FinalTransform = new ModelTransform3D();
			Scale = new Vector3F(1, 1, 1);
			Rotation = QuaternionF.Rotation(1, 0, 0, 0);
		}

		public List<SpaceBody> Satellites { get; private set; }
		public IEnumerable<SpaceBody> GetSystem()
		{
			yield return this;
			foreach (var item in Satellites)
				foreach (var subitem in item.GetSystem())
					yield return subitem;
		}

		public Shape Shape { get; set; }
		public Texture2D Texture { get; set; }
		public Material Material { get; set; }

		public Vector3F Scale { get; set; }
		public Vector3F Location { get; set; }
		public QuaternionF Rotation { get; set; }
		public Action<SpaceBody, PerformanceTimer> UpdateAction { get; set; }
		public ModelTransform3D FinalTransform { get; private set; }

		#region Update()

		public void Update(SpaceBody parent, PerformanceTimer t)
		{
			if (UpdateAction != null)
				UpdateAction(this, t);

			FinalTransform.Scale = Scale;
			FinalTransform.Rotation = Rotation;
			FinalTransform.Location = parent != null ? parent.FinalTransform.Location + Location : Location;
			if (Shape != null)
				FinalTransform.LeftHanded = Shape.LeftHanded;

			foreach (var item in Satellites)
			{
				var mu = item as SpaceBody;
				if (mu != null)
					mu.Update(this, t);
			}
		}

		#endregion
	}
}
